//
//  WBMeituanRefreshView.swift
//  GitWeibo
//
//  Created by 胡金东 on 2017/6/14.
//  Copyright © 2017年 胡金东. All rights reserved.
//

import UIKit

class WBMeituanRefreshView: RefreshView {

    /// - 环境图片
    @IBOutlet var buildingIcon: UIImageView!
    /// - 地球图片
    @IBOutlet var earthIcon: UIImageView!
    /// - 袋鼠图片
    @IBOutlet var kangarooIcon: UIImageView!
    
    /// - 父视图高度（从父视图中向子视图中的子类传递）
    override var parentViewheight: CGFloat{
        didSet{
            print("父视图的高度\(parentViewheight)")
            
            /// - 23是Y的偏移量
            if parentViewheight < 23 {
                return
            }
            /// - 23 -> 120
            /// - 0.2 -> 1
            /// - 23 = 0.2
            /// - 120 = 1
            var scale:CGFloat
            
            if parentViewheight > 120 {
                scale = 1
            } else{
                scale = 1 - (120 - parentViewheight) / (120-23)
            }
            kangarooIcon.transform = CGAffineTransform(scaleX: scale, y: scale)
            
        }
    }
    override func awakeFromNib() {
    
        let building1 = #imageLiteral(resourceName: "icon_building_loading_1")
        let building2 = #imageLiteral(resourceName: "icon_building_loading_2")
        /// - 环境图片切换动画
        buildingIcon.image = UIImage.animatedImage(with: [building1,building2], duration: 0.5)
        /// - 地球转动
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = -2 * M_PI
        animation.repeatCount = MAXFLOAT
        animation.duration = 2
        /// - 不让动画失效
        animation.isRemovedOnCompletion = false
        
        earthIcon.layer.add(animation,forKey:nil)
        
        /// - 设置袋鼠
        
        let kangaroo1 = #imageLiteral(resourceName: "icon_small_kangaroo_loading_1")
        let kangaroo2 = #imageLiteral(resourceName: "icon_small_kangaroo_loading_2")
        kangarooIcon.image = UIImage.animatedImage(with: [kangaroo1,kangaroo2], duration: 0.25)
        
        /// - 设置定位点 站在地球上
        kangarooIcon.layer.anchorPoint = CGPoint(x: 0.5, y: 1)
        /// - 设置中心点
        let X = self.bounds.width * 0.5+10
        let Y = self.bounds.height - 23
        kangarooIcon.center = CGPoint(x: X, y: Y)
        
        kangarooIcon.transform = CGAffineTransform(scaleX: 0.2, y: 0.2)
        
        
    }
}
